package wiimote;

import wiiusej.WiiUseApiManager;
import wiiusej.Wiimote;
import wiiusej.values.GForce;
import wiiusej.values.Orientation;
import wiiusej.values.RawAcceleration;
import wiiusej.wiiusejevents.physicalevents.ExpansionEvent;
import wiiusej.wiiusejevents.physicalevents.IREvent;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
import wiiusej.wiiusejevents.utils.WiimoteListener;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;

public class Controle implements WiimoteListener {

	protected Wiimote[] wiimotes;
	protected Wiimote wiimote;
	public int boutton;
	public float roulis;
	public float tangage;
	public float lacet;
	public float[] acc;
	public float[] force;

	public int N = 100;
	public int t;
	public float[] tamponRot;
	public float[] tamponTr;

	public Controle() {
		wiimotes = WiiUseApiManager.getWiimotes(1, false);
		wiimote = wiimotes[0];
		//wiimote.activateSmoothing();
		//wiimote.activateIRTRacking();
		wiimote.activateMotionSensing();
		wiimote.addWiiMoteEventListeners(this);

		boutton = 0;

		acc = new float[3];
		force = new float[3];

		this.t = 0;
		this.tamponRot = new float[N];
		this.tamponTr = new float[N];

		roulis = 0;
		tangage = 0;
		lacet = 0;
	}

	public void allumer(boolean a, boolean b) {
		wiimote.setLeds(a, b, false, false);
	}

	public void onButtonsEvent(WiimoteButtonsEvent arg0) {
		boutton = 0;
		if (arg0.isButtonLeftPressed()) {
			boutton = 1;
		} else if (arg0.isButtonRightPressed()) {
			boutton = 2;
		}

		else if (arg0.isButtonAPressed()) {
			boutton = 3;
		} else if (arg0.isButtonBPressed()) {
			boutton = 4;
		}

		else if (arg0.isButtonMinusPressed()) {
			boutton = 5;
		} else if (arg0.isButtonPlusPressed()) {
			boutton = 6;
		}

		else if (arg0.isButtonOnePressed()) {
			boutton = 7;
		} else if (arg0.isButtonTwoPressed()) {
			boutton = 8;
		}
	}

	public void onIrEvent(IREvent arg0) {
	}

	public void onMotionSensingEvent(MotionSensingEvent arg0) {
		RawAcceleration acceleration = arg0.getRawAcceleration();
		acc[0] = acceleration.getX();
		acc[1] = acceleration.getY();
		acc[2] = acceleration.getZ();
		GForce gforce = arg0.getGforce();
		force[0] = gforce.getX();
		force[1] = gforce.getY();
		force[2] = gforce.getZ();

		Orientation orientation = arg0.getOrientation();
		roulis = orientation.getARoll();
		tangage = orientation.getAPitch();

		tamponRot[t] = roulis;
		tamponTr[t] = tangage;
		t++;
		if (t >= N)
			t = 0;
	}

	public void onExpansionEvent(ExpansionEvent arg0) {
	}

	public void onStatusEvent(StatusEvent arg0) {
	}

	public void onDisconnectionEvent(DisconnectionEvent arg0) {
	}

	public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) {
	}

	public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) {
	}

	public void onClassicControllerInsertedEvent(
			ClassicControllerInsertedEvent arg0) {

	}

	public void onClassicControllerRemovedEvent(
			ClassicControllerRemovedEvent arg0) {

	}

	public void onGuitarHeroInsertedEvent(GuitarHeroInsertedEvent arg0) {

	}

	public void onGuitarHeroRemovedEvent(GuitarHeroRemovedEvent arg0) {

	}
}
